<?php

namespace App\Livewire\Admin\Permission;

use Livewire\Attributes\Layout;
use Livewire\Component;
use Spatie\Permission\Models\Permission;

#[Layout('components.admin.layout')]
class Update extends Component
{
    public $name;
    public $description;
    public $group;
    public Permission $permission;

    public function mount(Permission $permission) {
        $this->permission = $permission;
        $this->fill($permission->only('name', 'description', 'group'));
    }

    public function store()
    {
        $this->permission->name = $this->name;
        $this->permission->description = $this->description;
        $this->permission->group = $this->group;
        $res = $this->permission->save();
        if ($res) {
            $html = view('admin.permission.list-item', ['permission' => Permission::find($this->permission['id'])])->render();
            $this->dispatch('update-row', [
                'html' => $html,
                'id'  => $this->permission['id'],
            ]);
            $this->dispatch('close-modal');
        }
    }

    public function render()
    {
        return view('livewire.admin.permission.update');
    }
}
